// 获取项目ID
function getProjectId(src) {
    console.log(src);
    var qs = src.split('?')[1];
    var args = {};
    var items = qs.length ? qs.split('&') : [];
    var len = items.length;
    if (qs.length == 0) {
        // alert("没有查询字符串,退出！");
        return;
    }
    for (var i = 0; i < len; i++) {
        var item = items[i].split("=");
        args[decodeURIComponent(item[0])] = decodeURIComponent(item[1]);
    }
    return args['projectId'];
}
var timestamp = (new Date()).valueOf();
var pWindow = window.parent;
var sSrc = $('#main-content iframe', pWindow.document).attr('src');
var token = window.localStorage.getItem("token");
var id = getProjectId(sSrc);    // 项目id
console.log(id);
var user = getUser();
var conferenceStaff = [];
$(function () {
    var pWindow = window.parent;
    var sSrc = $('#main-content iframe', pWindow.document).attr('src');
    var token = window.localStorage.getItem("token");
    var id = getProjectId(sSrc);    // 项目id
    console.log(id);

    // 时间控件
    $('#inp-date-start').datetimepicker({
        language: 'zh-CN',
        format: 'yyyy-mm-dd hh:ii',
        autoclose: true
    });

    $("#file-pic").fileinput({
        showUpload: false,
        // showRemove: false,
        uploadAsync: true,
        showPreview: false,
        language: 'zh',
        textEncoding: "UTF-8",
        uploadUrl: baseURL + "generator/conferencesummary/upload",
        maxFileSize: 16300, /*设置允许图片大小单位KB*/
        maxFileCount: 1, /*允许最大上传数，可以多个，当前设置单个*/
        enctype: 'multipart/form-data',
        // allowedFileExtensions: ["jpg", "png", "gif", "pdf"], /*上传文件格式*/
        msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！",
        dropZoneTitle: "请通过拖拽图片文件放到这里",
        dropZoneClickTitle: "或者点击此区域添加图片",
        uploadExtraData: function (previewId, index) {
            var data = {
                conferenceId:timestamp,
                token: token,
                creationUser:user.userId,
                conferenceMesgAccessory:'1'
            };
            return data;
        },//这个是外带数据
        // showBrowse: false,
        browseOnZoneClick: true,
        slugCallback: function (filename) {
            return filename.replace('(', '_').replace(']', '_');
        }
    });
});

var vm = new Vue({
        el: '#rrapp',
        data: {
            showList: true,
            title: null,
            conferenceSummary: {}
        },
        mounted: function () {

            $.get(baseURL + 'sys/user/queryAll', {token: token}, function (result) {
                console.log(result);
                $('.select2').select2({
                    data: result.userList,
                    placeholder: '请选择',//默认文字提示
                    language: "zh-CN",//汉化
                    allowClear: true,//允许清空
                    multiple: true //启用多选
                });
            });
        },
        methods: {
            //提交
            saveOrUpdate: function (event) {
                var layerIndex = layer.load(1);
                var conferenceTime = $("#inp-date-start").val();
                conferenceStaff = $(".select2").val();    //多选
                if (conferenceStaff != null && conferenceStaff.length != 0) {
                    vm.conferenceSummary.conferenceStaff = conferenceStaff.join(',');
                }
                vm.conferenceSummary.id = timestamp,
                vm.conferenceSummary.conferenceTime = conferenceTime;
                vm.conferenceSummary.projectId = id;
                vm.conferenceSummary.creationUser = user.userId;
                if (!vm.conferenceSummary.conferenceTime) {
                    alert("请选择会议时间！");
                    layer.close(layerIndex);
                    return;
                }
                if (!vm.conferenceSummary.conferenceTheme) {
                    alert("会议主题不能为空！");
                    layer.close(layerIndex);
                    return;
                }
                if (!vm.conferenceSummary.conferenceStaff){
                    alert("请选择参会人员！");
                    layer.close(layerIndex);
                    return;
                }
                if($('#file-pic').fileinput("getFilesCount")>0){
                    $("#file-pic").fileinput("upload");
                }
                $.ajax({
                    type: "POST",
                    url: baseURL + "generator/conferencesummary/addConferenceSummary",
                    headers: {'token': token},
                    contentType: "application/json",
                    data: JSON.stringify(vm.conferenceSummary),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                layer.close(layerIndex);
                                var pWindow = window.parent;
                                // 改变首页的 indexVm 的 data，触发 updated
                                pWindow.indexVm.main = 'modules/project/projectdetails.html?projectId=' + id +'&operateType='+3;
                                pWindow.location.hash = 'modules/project/projectdetails.html?projectId=' + id+'&operateType='+3;
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            },
             //返回
            reload: function () {
                var pWindow = window.parent;
                // 改变首页的 indexVm 的 data，触发 updated
                pWindow.indexVm.main = 'modules/project/projectdetails.html?projectId=' + id+'&operateType='+3;
                pWindow.location.hash = 'modules/project/projectdetails.html?projectId=' + id+'&operateType='+3;
            }
        }
    })
;